<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 15-1-8
 * Time: 下午6:00
 * 预约管理
 */
namespace Factory\Controller;
use Mcit\Page;

class BespeakController extends BaseController {

    //首页
    public function index(){

        //2---厂商  3---运营商
        if(session("fac_type") == 2){
            if (session('fac_key')){
                $condition_str="b.ev_fac_code='".session('fac_key')."' and c.yy_user_id='".session('uid')."'";
            }else{$condition_str="b.ev_fac_code='".session('fac_login_name')."'";}
        }elseif(session("fac_type") == 3){
            if(session('pid') != 0){
                $operation = "c.plant_attr=3 and c.yy_user_id='".session('uid')."'";
            }else{
                $yyuserid = M()->query("select id from ev_fac where id='".session('uid')."' OR pid='".session('uid')."'");
                foreach($yyuserid as $k=>$v){
                    $yyuserids .= $v['id'].",";
                }
                $yyuserids = trim($yyuserids,',');
                $operation = "c.plant_attr=3 and c.yy_user_id IN($yyuserids)";
            }
        }

        $key_link = I('key_link');
        $location = I('location_f_1').",".I('location_s_1');
        if(!empty($_GET['key_link'])){
            $sql = "select * from char_state_now a
                LEFT JOIN ev_char b
                on b.key_link = a.key_link
                LEFT JOIN ev_plant c
                on c.id = b.ev_plant_id
                where {$condition_str} {$operation}
                and a.bespeak = 1
                and a.error = 0
                and a.cable = 0
                and charge = 0
                and a.key_link like '$key_link%'";
        }elseif(!empty($_GET['location_s_1'])){
            $sql = "select * from char_state_now a
                LEFT JOIN ev_char b
                on b.key_link = a.key_link
                LEFT JOIN ev_plant c
                on c.id = b.ev_plant_id
                where {$condition_str} {$operation}
                and a.bespeak = 1
                and a.error = 0
                and a.cable = 0
                and charge = 0
                and c.location like '$location%'";
        }else{
            $sql = "select * from char_state_now a
                LEFT JOIN ev_char b
                on b.key_link = a.key_link
                LEFT JOIN ev_plant c
                on c.id = b.ev_plant_id
                where {$condition_str} {$operation}
                and a.bespeak = 1
                and a.error = 0
                and a.cable = 0
                and charge = 0";
        }
        $count = count(M()->query($sql));
        //分页
        vendor('Mcit.Sys.Page');
        $Page = new Page($count, 20);
        $pageView = $Page->show();
        $this->assign('pageView', $pageView); //发送分页
        $nowPage = isset($_GET['page']) ? $_GET['page'] * 20 - 20 : 0;
        if(!empty($_GET['key_link'])){
            $sql1 = "select a.id,c.name,b.evc_number,a.key_link,a.now_user,c.location from char_state_now a
                LEFT JOIN ev_char b
                on b.key_link = a.key_link
                LEFT JOIN ev_plant c
                on c.id = b.ev_plant_id
                where  {$condition_str} {$operation}
                and a.bespeak = 1
                and a.charge = 0
                and a.cable = 0
                and a.error = 0
                and a.key_link like '$key_link%'
                limit $nowPage,".$Page->listRows;
        }elseif(!empty($_GET['location_s_1'])){
            $sql1 = "select a.id,c.name,b.evc_number,a.key_link,a.now_user,c.location from char_state_now a
                LEFT JOIN ev_char b
                on b.key_link = a.key_link
                LEFT JOIN ev_plant c
                on c.id = b.ev_plant_id
                where  {$condition_str} {$operation}
                and a.bespeak = 1
                and a.charge = 0
                and a.cable = 0
                and a.error = 0
                and c.location like '$location%'
                limit $nowPage,".$Page->listRows;
        }else{
            $sql1 = "select a.id,c.name,b.evc_number,a.key_link,a.now_user,c.location from char_state_now a
                LEFT JOIN ev_char b
                on b.key_link = a.key_link
                LEFT JOIN ev_plant c
                on c.id = b.ev_plant_id
                where {$condition_str} {$operation}
                and a.bespeak = 1
                and a.charge = 0
                and a.cable = 0
                and a.error = 0
                limit $nowPage,".$Page->listRows;
        }
        $charState = M()->query($sql1);
//        echo M()->getLastSql();
        foreach($charState as $k => $v){
            //预约开始时间
            $charState[$k]['btime'] = D("Admin/Bespeak")->where("key_link='".$v['key_link']."' AND user_card='".$v['now_user']."' AND type=1")->order("time desc")->getField("time");
            //当前电桩在线情况
            $onl  = D("Admin/Online")->where("key_link='".$v['key_link']."'")->find();
            if((strtotime("now")-strtotime($onl['time_last'])) >  240 && (strtotime("now")-strtotime($onl['time_last'])) <  600){//大于4分钟小于10分钟为异常
                $charState[$k]['status'] = '<span style="color: #cc6600">异常</span>';
            }else if((strtotime("now")-strtotime($onl['time_last'])) >  600){//大于60分钟为下线
                $charState[$k]['status'] = '<span style="color: red">下线</span>';
            }else{
                $charState[$k]['status'] = '<span style="color: #009900">在线</span>';
            }
            $region    = strsToArray($v['location']);
            $regionOne = D("Admin/Region")->where("code='".$region[2]."'")->field("fullname")->find();
            $charState[$k]['location'] = $regionOne['fullname'];
            if(mb_strlen($v['name'],"utf-8") <= 6){
                $charState[$k]['name_text'] = $v['name'];
            }else{
                $charState[$k]['name_text'] = mb_substr($v['name'],0,6,"utf-8")."···";
            }
        }
        $this->charState  = $charState;
        $this->china = D("Admin/Region")->getChinaArea();
        $this->display();
    }
	
	//预约详情
	public function detail(){
        $stateId    = I("id");
        $states     = D("Admin/State")->Relation(true)->where("id='".$stateId."'")->find();
        $charInfo   = M("char")->where("key_link='".$states['key_link']."'")->find();
        $plantInfo  = M("plant")->where("id='".$charInfo['ev_plant_id']."'")->find();
        //预约开始时间
        $states['btime'] = D("Admin/Bespeak")->where("key_link='".$states['key_link']."' AND user_card='".$states['now_user']."' AND type=1")->order("time desc")->getField("time");
        $states['charInfo']   = $charInfo;
        $states['plantInfo']  = $plantInfo;
        $this->data = $states;
		$this ->display();
    }
}
